//
//  KMViewStore.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//

#import "KMConfigStore.h"
#import "KDDHeader.h"
#import "KDDModel.h"
#import "NSObject+KMModel.h"
#import "KDDRegisterInfo.h"
#import "KDDCollectionWrapper.h"
#import "KDDConfigStore.h"
#import "KMContext.h"

@implementation KMConfigStore

+ (NSMutableDictionary<NSString *, Class<KMDecorationView>> *)sharedDecorationStore
{
    static NSMutableDictionary<NSString *, Class<KMDecorationView>> * decorationStore = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        decorationStore = [NSMutableDictionary dictionary];
    });
    return decorationStore;
}

+ (void)registerModelView:(Class<KMView>)viewClass
{
    KMViewConfig * viewConfig = [[KMViewConfig alloc] initWithViewClass:viewClass];
    [[KDDConfigStore sharedStore] addModelConfig:viewConfig];
}

+ (KMViewConfig *)modelConfigForViewID:(NSString *)viewID
{
    return (KMViewConfig *)[[KDDConfigStore sharedStore] modelConfigWithID:viewID];
}

+ (void)registerDecorationView:(Class<KMDecorationView>)viewClass
{
    [self sharedDecorationStore][[viewClass viewID]] = viewClass;
}

+ (Class<KMDecorationView>)decorationForViewID:(NSString *)viewID
{
    return [self sharedDecorationStore][viewID];
}

+ (BOOL)configModel:(NSObject *)model module:(KUIModule *)module
{
    KMViewConfig * config = nil;
    if (model.ddConfigID == nil)
    {
        model.ddConfigID = [module.viewIDValue execute:model];
    }
    if (model.ddConfigID != nil)
    {
        config = [self modelConfigForViewID:model.ddConfigID];
        if (config != nil)
        {
            //需要代理 未指定代理时 默认以module作为代理
            if (model.kmDelegate == nil && config.needDelegate)
            {
                model.kmDelegate = [module appointedDelegateForViewID:model.ddConfigID] ?: module;
            }
            if (model.ddViewType == KDDViewTypeNone)
            {
                if (module.viewTypeValue != nil)
                {
                    model.ddViewType = [[module.viewTypeValue execute:model] unsignedIntegerValue];
                }
                if (model.ddViewType == KDDViewTypeNone)
                {
                    model.ddViewType = KDDViewTypeCell;
                }
            }
            if (model.ddDecorationID != nil || module.decorationIDValue != nil)
            {
                [self configDecorationIDForModel:model module:module];
            }
            [config configModel:model];
            return YES;
        }
    }
    NSLog(@"FatalError!!! invalidModel:%@ viewID:%@",model,model.ddConfigID);
    return NO;
}

+ (void)configDecorationIDForModel:(NSObject *)model module:(KUIModule *)module
{
    KMValue * decorationIDValue = module.decorationIDValue;
    if (![model.ddDecorationID containsString:@"__"])
    {
        NSString * decorationID = model.ddDecorationID ?: [decorationIDValue execute:model];
        Class<KMDecorationView> decorationClass = nil;
        if (decorationID.length > 0)
        {
            decorationClass = [KMConfigStore decorationForViewID:decorationID];
        }
        if (decorationClass != nil)
        {
            NSString * spec = nil;
            NSString * reuseID = decorationID;
            if ([(id)decorationClass respondsToSelector:@selector(specForModel:)])
            {
                spec = [(Class<KMDecorationView>)decorationClass specForModel:model];
                reuseID = [NSString stringWithFormat:@"%@__%@",decorationID,spec];
            }
            model.ddDecorationID = reuseID;
            
            if ([[module.context dataDriven].configure registerInfoForReuseID:reuseID viewType:KDDViewTypeDecoration] == nil)
            {
                UIEdgeInsets insets = UIEdgeInsetsZero;
                NSInteger zIndex = -1;
                if ([(id)decorationClass respondsToSelector:@selector(insetsForSpec:)])
                {
                    insets = [(Class<KMDecorationView>)decorationClass insetsForSpec:spec];
                }
                if ([(id)decorationClass respondsToSelector:@selector(zIndexForSpec:)])
                {
                    zIndex = [(Class<KMDecorationView>)decorationClass zIndexForSpec:spec];
                }
                [[module.context dataDriven].configure registerDecorationView:[KDDCollectionDecorationView class] insets:insets zIndex:zIndex reuseID:reuseID];
            }
        }
        else if (model.ddDecorationID != nil)
        {
            model.ddDecorationID = nil;
        }
    }
}

@end
